package com.judian.smart.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.judian.smart.R;
import com.judian.smart.constant.Constants;
import com.judian.smart.holder.HaveDeviceViewHolder;
import com.judian.smart.interfaces.OnListItemClickListener;
import com.judian.smart.model.DeviceType;

import java.util.List;

/**
 * author: Peter
 * date: 2019/08/09 星期五 10:12
 * desc:
 */
public class HaveDeviceListAdapter extends RecyclerView.Adapter<HaveDeviceViewHolder> {
    private Context context;
    private List<DeviceType> mData;
    public OnListItemClickListener mListener;
    public int pSelectPosition = 0;

    public HaveDeviceListAdapter(Context context, List<DeviceType> data) {
        this.context = context;
        this.mData = data;
    }

    public void setListener(OnListItemClickListener listener) {
        mListener = listener;
    }


    @NonNull
    @Override
    public HaveDeviceViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new HaveDeviceViewHolder(LayoutInflater.from(context).inflate(R.layout.item_home, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull HaveDeviceViewHolder holder, final int position) {
        //设置设备名称
        holder.haveDeviceName.setText(mData.get(position).getDeviceTypeName());
        //设置点击回调
        holder.haveDeviceLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                mListener.setOnItemClick(mData.get(position), position);
            }
        });

        if (position == pSelectPosition) {
            holder.haveDeviceName.setTextColor(Color.WHITE);
            holder.haveDeviceLayout.setBackgroundResource(R.mipmap.room_click);
            int deviceTypeKey = mData.get(position).getDeviceTypeKey();
            switch (deviceTypeKey) {
                case Constants.DEVICE_TYPE_LIGHT:
                    //灯光
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.light_click);
                    break;
                case Constants.DEVICE_TYPE_CURTAIN:
                    //窗帘
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.curtain_click);
                    break;
                case Constants.DEVICE_TYPE_SOCKET:
                    //插座
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.socket_click);
                    break;
                case Constants.DEVICE_TYPE_CONDITIONING:
                    //空调
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.conditioning_click);
                    break;
                case Constants.DEVICE_TYPE_TV:
                    //电视
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.tv_click);
                    break;
                case Constants.DEVICE_TYPE_INFRARED:
                    //红外
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.infrared_click);
                    break;
                case Constants.DEVICE_TYPE_DIMMER:
                    //调光
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.dimmer_click);
                    break;
                case Constants.DEVICE_TYPE_MUSIC:
                    //音乐
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.music_click);
                    break;
                case Constants.DEVICE_TYPE_LOCK:
                    //电子锁
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.lock_click);
                    break;
                case Constants.DEVICE_TYPE_PROJECTOR:
                    //投影仪
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.projector_click);
                    break;
                case Constants.DEVICE_TYPE_INTELLIGENTOPEN:
                    //智能空开
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.air_click);
                    break;
                case Constants.DEVICE_TYPE_TIMESOCKET:
                    //时序开关
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.time_click);
                    break;
                case Constants.DEVICE_TYPE_FRESHAIRSYSTEM:
                    //新风系统
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.freshairsystem_click);
                    break;
                case Constants.DEVICE_TYPE_ELECTRICAL:
                    //家电
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.electrical_click);
                    break;
            }
        } else {
            holder.haveDeviceName.setTextColor(Color.rgb(131, 186, 255));
            holder.haveDeviceLayout.setBackgroundResource(R.mipmap.room_not_click);
            int deviceTypeKey = mData.get(position).getDeviceTypeKey();
            switch (deviceTypeKey) {
                case Constants.DEVICE_TYPE_LIGHT:
                    //灯光
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.light_click_not);
                    break;
                case Constants.DEVICE_TYPE_CURTAIN:
                    //窗帘
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.curtain_click_not);
                    break;
                case Constants.DEVICE_TYPE_SOCKET:
                    //插座
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.socket_click_not);
                    break;
                case Constants.DEVICE_TYPE_CONDITIONING:
                    //空调
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.conditioning_click_not);
                    break;
                case Constants.DEVICE_TYPE_TV:
                    //电视
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.tv_click_not);
                    break;
                case Constants.DEVICE_TYPE_INFRARED:
                    //红外
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.infrared_click_not);
                    break;
                case Constants.DEVICE_TYPE_DIMMER:
                    //调光
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.dimmer_click_not);
                    break;
                case Constants.DEVICE_TYPE_MUSIC:
                    //音乐
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.music_click_not);
                    break;
                case Constants.DEVICE_TYPE_LOCK:
                    //电子锁
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.lock_click_not);
                    break;
                case Constants.DEVICE_TYPE_PROJECTOR:
                    //投影仪
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.projector_click_not);
                    break;
                case Constants.DEVICE_TYPE_INTELLIGENTOPEN:
                    //智能空开
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.air_click_not);
                    break;
                case Constants.DEVICE_TYPE_TIMESOCKET:
                    //时序开关
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.time_click_not);
                    break;
                case Constants.DEVICE_TYPE_FRESHAIRSYSTEM:
                    //新风系统
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.freshairsystem_click_not);
                    break;
                case Constants.DEVICE_TYPE_ELECTRICAL:
                    //家电
                    holder.haveDeviceImage.setBackgroundResource(R.mipmap.electrical_click_not);
                    break;
            }
        }
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }
}
